﻿using FastCloud.Base;
using FastCloud.Base.Config;
using FastCloud.ORG.DB;
using FastCloud.ORG.Entity;
using FastIOC.Attribute;
using FastUtil.Convert;
using FastUtil.Data;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.ORG
{
    [Route("frame/home")]
    public class HomeController : BaseController
    {
        [AutoWired]
        private DB_Login @DB_Login;
        [AutoWired]
        private DB_User @DB_User;
        [AutoWired]
        private DB_OU @DB_OU;
        [AutoWired]
        private DB_Tenant @DB_Tenant;
        [AutoWired]
        private TokenUtil @TokenUtil;

        [Route("index")]
        public IActionResult Index()
        {
            return View("~/Views/Home/Home.cshtml");
        }

        [Route("login")]
        public IActionResult Login()
        {
            return View("~/Views/Home/Login.cshtml");
        }

        [Route("userlogin")]
        public IActionResult UserLogin()
        {
            string LoginID = ValueConvert.ConvertToString(GetFormParam("LoginID"));
            string Password = ValueConvert.ConvertToString(GetFormParam("Password"));
            DB_Login.Add2Parameter("LoginID", LoginID);
            Frame_Login LoginEntity = DB_Login.SelectOne("*", "LoginID=@LoginID");
            if (string.IsNullOrEmpty(LoginID) || string.IsNullOrEmpty(Password))
            {
                return Result.Error("请输入用户名密码！");
            }
            if (LoginEntity != null)
            {
                if (LoginEntity.Enable == (int)FastCloud.Base.Enum.Common.EnableOrDisable.启用 && LoginEntity.AllowManage == (int)FastCloud.Base.Enum.Common.EnableOrDisable.启用)
                {
                    string MD5Password= EncryptUtil.MD5Encrypt(LoginID + "|" + Password);
                    if (LoginEntity.Password == MD5Password)
                    {
                        Frame_User UserEntity = DB_User.SelectOne(LoginEntity.UserGuid);
                        Frame_OU OUEntity = DB_OU.SelectOne(UserEntity.OUGuid);
                        Frame_Tenant TenantEntity = DB_Tenant.SelectOne(OUEntity.TenantID);
                        string Token = TokenUtil.CreateToken(UserEntity.UserGuid, UserEntity.LoginID, UserEntity.DisplayName, UserEntity.Mobile,
                            OUEntity.OUGuid, OUEntity.OUName, OUEntity.OUCode, OUEntity.ParentOUGuid, TenantEntity.TenantID, TenantEntity.TenantName);
                        Hashtable result = new Hashtable();
                        result.Add("Toekn", Token);
                        result.Add("RedirectUrl", CloudUtil.GetRootPath() + "frame/home/index");
                        return Result.OK(result);
                    }
                    else
                    {
                        return Result.Error("密码不正确！");
                    }
                }
                else
                {
                    return Result.Error("用户无权限！");
                }

            }
            else
            {
                return Result.Error("用户不存在！");
            }
        }

    }
}
